package fr.esiag.isidis.epp.repository;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import au.com.bytecode.opencsv.CSVReader;
import fr.esiag.isidis.epp.model.CDCModel;
import fr.esiag.isidis.epp.model.WeightModel;
import fr.esiag.isidis.epp.util.Utils;

@Repository
public class CDCAndWeightRepository {

	private static Logger logger = Logger.getLogger(CDCAndWeightRepository.class);
	
	@Value("${batch.in.cdc.file}")
	private String cdc_pathCsvFile;
	
	@Value("${batch.in.weight.file}")
	private String weight_pathCsvFile;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int loadCdc(CDCModel cdcItem)
	{
			
		/* traitement cdc */
		int date = Utils.convertDateToInt(cdcItem.getDates());
		int heure = Utils.convertHourToInt(cdcItem.getDates());
		int id;
		String idnn;
		int charge;
		Vector<Integer> tabCharge = cdcItem.getCharge();
		// weight
		int result = 0;
		for(id=0; id< 42; id++){
			idnn = Utils.getIdnn(id);
			if(tabCharge.get(id)!= null){
				charge = tabCharge.get(id);
				Object[] params = {idnn,date,heure,charge,null};
				result += jdbcTemplate.update(SqlConstants.QUEYR_INSERT_INTO_CDC_WEIGHT, params);
			}
			else{
				Object[] params = {idnn,date,heure,null,null};
				result += jdbcTemplate.update(SqlConstants.QUEYR_INSERT_INTO_CDC_WEIGHT, params);
			}
			// table poids 
			//	
//			IdSite 		VARCHAR(8) NOT NULL PRIMARY KEY,
//			PDate 		int NOT NULL,
//			PHour		int NOT NULL,
//			Power		int,
//			Weight 		float,
//			INDEX temperature_index (PDate)

		}
		return result;
	}
	
	public int loadWeight(WeightModel weightItem)
	{
		/* traitement weight */
		int date = Utils.convertDateToInt(weightItem.getDate());
		int heure = Utils.convertHourToInt(weightItem.getDate());
		String id = weightItem.getId();
		float weight = weightItem.getWeight();
			// table poids 
		//	
//		IdSite 		VARCHAR(8) NOT NULL PRIMARY KEY,
//		PDate 		int NOT NULL,
//		PHour		int NOT NULL,
//		Power		int,
//		Weight 		float,
//		INDEX temperature_index (PDate)
			Object[] params = {
					id,
					date,
					heure,
					null,
					weight
			};
		int	result = jdbcTemplate.update(SqlConstants.QUEYR_INSERT_INTO_CDC_WEIGHT, params);
		return result;
	}
	
	public List<CDCModel> cdc_extractFromCsvFile() throws Exception
	{
		List<CDCModel> retour = new ArrayList<CDCModel>();
		
		// Traitement
		CSVReader reader = new CSVReader(new FileReader(cdc_pathCsvFile),';');
		// Header
		String[] nextLine = reader.readNext();
		nextLine = reader.readNext();
		
		while(nextLine != null)
		{
			
			CDCModel item = cdc_transformeCDCFromCsvLine(nextLine);
			retour.add(item);
			logger.debug("cdc "+item);
			nextLine = reader.readNext();
		}
		reader.close();
		return retour;
	}
	
	private CDCModel cdc_transformeCDCFromCsvLine(String[] items)
	{
		CDCModel retour = new CDCModel();
		retour.setDates(items[0]);
		Vector<Integer> vect = new Vector<Integer>();
		int i;
		for (i= 1; i<=43; i++){
			String a = items[i];
			if ( !a.equals("")){
				vect.add(Integer.parseInt(a));
			}
			else
				vect.add(null);
		}
		retour.setCharge(vect);
		return retour;
	}
	
	
	/* ------------------Weight -------------------------*/
	public List<WeightModel> weight_extractFromCsvFile() throws Exception
	{
		List<WeightModel> retour = new ArrayList<WeightModel>();
		
		// Traitement
		CSVReader reader = new CSVReader(new FileReader(weight_pathCsvFile),';');
		// Header
		//Id site;Date;Poids
		String[] nextLine = reader.readNext();
		nextLine = reader.readNext();
		
		while(nextLine != null)
		{
			
			WeightModel item = weight_transformeCDCFromCsvLine(nextLine);
			retour.add(item);
			logger.debug("weight "+item);
			nextLine = reader.readNext();
		}
		reader.close();
		return retour;
	}
	
	
	private WeightModel weight_transformeCDCFromCsvLine(String[] items)
	{
		WeightModel retour = new WeightModel();
		//Id site;Date;Poids
		retour.setId(items[0]);
		retour.setDate(items[1]);
		String a = items[2];
			if(!a.equals("")){
				a = Utils.virguleTopoint(a);
		retour.setWeight(Float.parseFloat(a));
			}
			else{
				retour.setWeight(Float.NaN);
			}
		return retour;
	}
	
}
